<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>layui在线调试</title>
    <link rel="stylesheet" href="/ehome/static/css/layui.css" media="all">
    <style>
        body{margin: 10px;}
        .demo-carousel{height: 200px; line-height: 200px; text-align: center;}
    </style>
</head>
<body>
<div class="demoTable">
    请输入搜索条件：
    <div class="layui-inline">
        <input class="layui-input" name="id" id="demoReload" autocomplete="off">
    </div>
    <button class="layui-btn" data-type="reload">搜索</button>
</div>
<br>

<table class="layui-hide" id="demo" lay-filter="test"></table>

<!--加入模糊查询-->

<script type="text/html" id="toolbarDemo">
   <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="delete">批量删除</button>
        <button class="layui-btn layui-btn-sm" lay-event="update">编辑</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="/ehome/static/layui.js"></script>
<script>
    layui.config({
        version: '1593348928630' //为了更新 js 缓存，可忽略
    });

    layui.use(['laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'slider','jquery'], function(){
        var $=layui.jquery
            ,laydate = layui.laydate //日期
            ,laypage = layui.laypage //分页
            ,layer = layui.layer //弹层
            ,table = layui.table //表格
            ,carousel = layui.carousel //轮播
            ,upload = layui.upload //上传
            ,element = layui.element //元素操作
            ,slider = layui.slider //滑块

        //向世界问个好


        //监听Tab切换
        element.on('tab(demo)', function(data){
            layer.tips('切换了 '+ data.index +'：'+ this.innerHTML, this, {
                tips: 1
            });
        });

        //执行一个 table 实例
        table.render({
            elem: '#demo'
            ,height: 465
            ,url: "/ehome/sysUser/selectAll" //数据接口
            ,type:"get"
            ,title: '用户数据表'
            ,page: true //开启分页
            ,limits:[4,8,10,12]
            ,limit:8      //每页默认显示的数量
            ,toolbar: '#toolbarDemo'
           // ,toolbar: 'default' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left', totalRowText: '合计：'}
                ,{field: 'jobNumber', title: '工号', width:80}
                ,{field: 'avatar', title: '工号', width:80}
                ,{field: 'userName', title: '姓名', width:120}
                ,{field: 'createdDate', title: '创建日期', width: 200, sort: true, totalRow: true}
                ,{field: 'phoneNumber', title: '电话', width:120, sort: true}
                ,{field: 'type', title: '类型', width:80}
                ,{field: 'idCard', title: '身份证', width: 200}
                ,{fixed: 'right',title:'操作', width: 165, align:'center', toolbar: '#barDemo'}
            ]]
        });
        //搜索框执行重载
        var $ = layui.$, active = {
            reload: function(){
                var demoReload = $('#demoReload');    //demoReload : 模糊查询输入框
                //执行重载
                table.reload('demo', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        key: {
                            id: demoReload.val()
                        }
                    }
                }, 'data');
            }
        };
        $('.demoTable .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        var  active = {
            reload: function(){
                var demoReload = $('#phone');
                $('.env_message').html('当前操作环境：'+ parent.window.env_tools);
                //执行重载
                table.reload('testReload1', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        phone: demoReload.val(),
                        env_tools:parent.window.env_tools
                    }
                });
            }
        };
    /*    table.on('toolbar(demo'),function(obj){
            switch(obj.event()){
                case'add':
                    lay.msg('添加');
                    break;
                case'delete':
                    var checkStatus=table.checkStatus('demo');
                    var idArray = new Array();
            }
        }*/
//批量删除操作
        table.on('toolbar(test)', function(obj){

            switch(obj.event){
                case 'add':
                    layer.msg('添加');
                    break;
                case 'delete':
                    var checkStatus = table.checkStatus('demo'); //demo 即为基础参数 id 对应的值
                    // console.log(checkStatus.data) //获取选中行的数据

                    // 准备一个容器, 装所有的id : idArray
                    var idArray = new Array();


                    // 遍历
                    for(var o in checkStatus.data){
                        idArray[o] = checkStatus.data[o]["id"];
                    }
                    console.log(JSON.stringify(idArray));

                    // 传递的目标:  [1,4,5]
                    $.ajax({
                        url:"/ehome/sysUser.do"
                       , headers:{token:"delAll"}
                        ,type:"post"
                        ,data:{idArray:JSON.stringify(idArray)}
                        ,dataType:"json"

                        ,success:function(res){
                            if(res.code==0){
                                layer.msg(res.msg)
                                setInterval(function(){
                                    location.reload(true);
                                },500)
                            }
                        }
                        ,error:function (e){
                            console.log(e)
                        }
                    })
                    break;
                case 'update':
                    layer.msg('编辑');
                    break;
            };
        });

//监听行工具事件
        //删除更新操作
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'detail'){
                layer.msg('查看操作');
            } else if(layEvent === 'del'){
                layer.confirm('确定删除？', function(index){

                    console.log(data.id)
                    //向服务端发送删除指令
                    $.ajax({
                        headers:{"token":"delete"},
                        url: "/ehome/sysUser.do"
                        ,type:"post"
                        ,dataType:"json"
                        ,data:{"id":data.id}   //{ID:JSON.stringify(obj.field)}
                        ,success:function (result) {
                            if (result.code=="0") {
                                layer.msg(result.msg)
                                obj.del(); //删除对应行（tr）的DOM结构
                                layer.close(index);
                            }else{
                                layer.msg("删除失败")
                            }
                        }
                    })
                });
            } else if(layEvent === 'edit'){
                layer.msg('编辑操作');

                layer.open({
                    type:2,
                    content:['update.html']
                    ,area: ['600px', '400px']
                    ,anim: 2
                    ,success: function(layero, index){
                        //console.log(layero, index);
                        var body = layer.getChildFrame('body', index);
                        //var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        //console.log(JSON.stringify(data));
                        body.find("input")[0].value=obj.data.companyId;
                        body.find("input")[1].value=obj.data.type;
                        body.find("input")[2].value=obj.data.id;
                        body.find("input")[3].value=obj.data.jobNumber;
                        body.find("input")[4].value=obj.data.password;
                        body.find("input")[5].value=obj.data.userName;
                        body.find("input")[6].value=obj.data.phoneNumber;
                        body.find("input")[7].value=obj.data.idCard;

                    }
                })
            }
        });
    });

</script>
</body>
</html>
